package com.vids.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vids.mapper.ApeMapper;
import com.vids.model.entity.Ape;
import com.vids.model.param.ApeBatchParam;
import com.vids.service.IApeService;
import com.vids.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/ape")
public class IpcController
{
    IApeService apeService;

    @Resource
    ApeMapper apeMapper;


    @PostMapping
    public Result<?> create(@RequestBody Ape ape)
    {
        log.info("create ape =============>"+JSON.toJSONString(ape));
        apeMapper.insert(ape);
//        apeService.insertDevice(ape);

        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Ape ape)
    {
        log.info("update ape =============>"+JSON.toJSONString(ape));
        apeMapper.updateById(ape);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id)
    {
        log.info("delete ape ============= id: "+id);
        apeMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Ape> wrapper = Wrappers.<Ape>lambdaQuery();

        log.info("search="+ search);
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Ape::getName, search);
            log.info("search="+ search);
        }

        Page<Ape> ApePage = apeMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);

        log.info("ApePage  total"+ ApePage.getTotal());
        log.info("ApePage  pages"+ ApePage.getPages());
        return Result.success(ApePage);
    }


}

//    @GetMapping(value = "/list", produces = "application/json;charset=UTF-8")
//    public Object getList(HttpServletRequest request)
//    {
//        log.info("getList");
//        Object attribute = request.getAttribute("map");
//        ApeBatchParam param = null;
//        if (attribute instanceof Map) {
//            Map<String, Object> map = (Map<String, Object>) attribute;
//            param = apeService.getDeviceList(map);
//        }
//        if (param == null) {
//            return new ResponseEntity<Map<String, Object>>(new HashMap<String, Object>(), HttpStatus.NO_CONTENT);
//        }
//        return param;
//    }

//    @PostMapping  ("/create")
//    @ResponseBody
//    public int createApe(@RequestBody ApeBatchParam apes)
//    {
////        Ape t = new Ape();
//        List<Ape>  listApe= apes.getAPEListObject().getAPEObject();
//        for (Ape e:listApe)
//        {
//
////            t.setId("42010000001190000448");
////            t.setApeID("42010000001190000448");
////            t.setName("测试摄像头");
////            t.setIsOnline("1");
////            t.setModel("Y-2");
////            t.setIPAddr("192.168.0.101");
////            t.setPort(80);
////            t.setLongitude(115.051376);
////            t.setLatitude(32.114000);
////            t.setPlaceCode("42020603");
////            t.setPlace("黄石公园");
////
////            t.setUserId("admin");
////            t.setPassword("123456");
//
//            apeService.insertDevice(e);
//        }
//
//        return 1;
//    }